using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Package;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Designer.Interfaces;
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Xml;
using System.Diagnostics;

using IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
using IServiceProvider = System.IServiceProvider;

namespace MH.WsdlWorks
{

    [CLSCompliant(false), ComVisible(true)]
    public class ProjectNodeBaseProperties : SimpleNodeProperties
    {
        #region properties
        [SRCategoryAttribute(SR.Misc)]
        [Microsoft.VisualStudio.Package.LocDisplayName(SR.ProjectFolder)]
        [SRDescriptionAttribute(SR.ProjectFolderDescription)]
        [AutomationBrowsable(false)]
        public string ProjectFolder
        {
            get
            {
                Debug.Assert(this.Node != null, "The associated hierarchy node has not been initialized");

                return this.Node.ProjectMgr.ProjectFolder;
            }
        }

        [SRCategoryAttribute(SR.Misc)]
        [Microsoft.VisualStudio.Package.LocDisplayName(SR.ProjectFile)]
        [SRDescriptionAttribute(SR.ProjectFileDescription)]
        [AutomationBrowsable(false)]
        public string ProjectFile
        {
            get
            {
                Debug.Assert(this.Node != null, "The associated hierarchy node has not been initialized");

                return this.Node.ProjectMgr.ProjectFile;
            }
            set
            {
                Debug.Assert(this.Node != null, "The associated hierarchy node has not been initialized");

                this.Node.ProjectMgr.ProjectFile = value;
            }
        }

        [SRCategoryAttribute(SR.Misc)]
        [DisplayName("SomeText")]
        [Description("Let's see if this works")]
        [AutomationBrowsable(false)]
        public string SomeText
        {
            get 
            { 
                return this.Node.ProjectMgr.SomeText; 
            }
            set 
            {
                this.Node.ProjectMgr.SomeText = value; 
            }
        }


        #region non-browsable properties - used for automation only
        [Browsable(false)]
        public string FileName
        {
            get
            {
                Debug.Assert(this.Node != null, "The associated hierarchy node has not been initialized");

                return this.Node.ProjectMgr.ProjectFile;
            }
            set
            {
                Debug.Assert(this.Node != null, "The associated hierarchy node has not been initialized");

                this.Node.ProjectMgr.ProjectFile = value;
            }
        }


        [Browsable(false)]
        public string FullPath
        {
            get
            {
                Debug.Assert(this.Node != null, "The associated hierarchy node has not been initialized");
                string fullPath = this.Node.ProjectMgr.ProjectFolder;
                if (!fullPath.EndsWith(Path.DirectorySeparatorChar.ToString()))
                {
                    return fullPath + Path.DirectorySeparatorChar;
                }
                else
                {
                    return fullPath;
                }
            }
        }
        #endregion

        #endregion

        #region ctors
        public ProjectNodeBaseProperties()
        {
        }

        public ProjectNodeBaseProperties(SimpleProjectNode node)
            : base(node)
        {
        }
        #endregion

        #region overridden methods
        public override string GetClassName()
        {
            return SR.GetString("ProjectProperties");
        }

        /// <summary>
        /// ICustomTypeDescriptor.GetEditor
        /// To enable the "Property Pages" button on the properties browser
        /// the browse object (project properties) need to be unmanaged
        /// or it needs to provide an editor of type ComponentEditor.
        /// </summary>
        /// <param name="editorBaseType">Type of the editor</param>
        /// <returns>Editor</returns>
        public override object GetEditor(Type editorBaseType)
        {
            // Override the scenario where we are asked for a ComponentEditor
            // as this is how the Properties Browser calls us
            if (editorBaseType == typeof(ComponentEditor))
            {
                IOleServiceProvider sp;
                ErrorHandler.ThrowOnFailure(this.Node.GetSite(out sp));
                return new PropertiesEditorLauncher(new ServiceProvider(sp));
            }

            return base.GetEditor(editorBaseType);
        }

        public override int GetCfgProvider(out IVsCfgProvider p)
        {
            Debug.Assert(this.Node != null, "The associated hierarchy node has not been initialized");

            if (this.Node != null && this.Node.ProjectMgr != null)
            {
                return this.Node.ProjectMgr.GetCfgProvider(out p);
            }

            return base.GetCfgProvider(out p);
        }
        #endregion
    }
}